package com.ocom.oauth.auth.aiotweb;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.aiot.EntityUserInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.IPUtils;
import com.ocom.oauth.client.AiotClientService;
import com.ocom.redis.util.RedisUtil;
import com.ocom.security.authentication.YoCiUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 *  说明：自定义的获取用户信息
 */
@Service
@Slf4j
public class AiotUserDetailService {
    @Autowired
    AiotClientService aiotClientService;

    @Autowired
    private RedisUtil redisUtil;


    //获取用户
    public UserDetails loadByNameAndCode(String account,String password, String verify) {


        String ip= IPUtils.getHostIp();

        Object redisObject=redisUtil.get("verification:" + verify);
        redisUtil.del("verification:" + verify);
        if (redisObject==null) {
            throw new InvalidGrantException("验证码错误或已过期1");
        }

        String redisCode= String.valueOf(redisObject) ;

        //在验证用户名和验证码--load from redis
        if (!redisCode.equals(verify)) {
            throw new InvalidGrantException("验证码错误或已过期2");
        }
        Result result=aiotClientService.GetUserInfo(account);
        log.info("用户信息",result);
        if(result.getCode()!=200){
            throw new UsernameNotFoundException("用户不存在或者密码错误");
        }

        EntityUserInfo entityUserInfo = JSON.parseObject(JSON.toJSONString(result.getData()), EntityUserInfo.class);

        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        String hashPass = password+entityUserInfo.getSalt();
        boolean b= bcryptPasswordEncoder.matches(hashPass,entityUserInfo.getPassword());
        if(!b){
            throw new UsernameNotFoundException("用户不存在或者密码错误");
        }

        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(String.valueOf(entityUserInfo.getUserType())));
        authorities.add(new SimpleGrantedAuthority("ROLE_AIOT_WEB"));

        Set<String> permissions = new HashSet<>();
        permissions.add("sys:user:admin");

        return new YoCiUser(entityUserInfo.getUserId(),
                (Long) entityUserInfo.getComId(),
                (Long) entityUserInfo.getUserType(),
                "0"
                , permissions,
                entityUserInfo.getAccount(),
                entityUserInfo.getPassword(), true, true, true, true, authorities,new ArrayList<>());
    }

}
